package com.developtools.utils;


import com.google.common.collect.Maps;
import com.intellij.psi.PsiElement;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.javadoc.PsiDocTag;
import com.intellij.psi.javadoc.PsiDocTagValue;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.deft.Obj;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author liujun
 */
public class PsiDocCommentUtils {

    public static final String PARAM = "param";

    public static Map<String, Object> toTagMap(PsiDocComment psiDocComment){
        if (psiDocComment == null){
            return Maps.newHashMap();
        }
        PsiDocTag[] tags = psiDocComment.getTags();
        if (tags.length == 0){
            return Maps.newHashMap();
        }
        Map<String, Object> map = new HashMap<>();
        for (PsiDocTag tag : tags) {
            if (Objects.isNull(tag)){
                continue;
            }
            String key = null;
            if (tag.getName().equals(PARAM)){
                PsiElement[] dataElements = tag.getDataElements();
                if (dataElements.length > 0){
                    key =  dataElements[0].getText();
                }
            }else{
                key = tag.getName();
            }
            if (tag.getName().equals(PARAM)){
                PsiElement[] dataElements = tag.getDataElements();
                if (dataElements.length > 1){
                    map.put(key, dataElements[1].getText()) ;
                }else {
                    map.put(key, "");
                }
            }else {
                PsiDocTagValue valueElement = tag.getValueElement();
                if (valueElement == null){
                    map.put(key, "");
                }else{
                    map.put(key, valueElement.getText());
                }
            }
        }
        return map;
    }

    public static String toDocText(PsiDocComment psiDocComment){
        if (psiDocComment == null){
            return "";
        }
        String text = psiDocComment.getText().replaceAll("\n", "");
        int index = text.indexOf("/**");
        if (index == 0){
            text = text.substring(2);
        }
        int in = text.indexOf("*/");
        if (in == text.length() -2){
            text = text.substring(0, text.length() -2);
        }

        String[] strArr = text.split("\\*");
        if (strArr.length == 0){
            return "";
        }
        StringBuilder builder = new StringBuilder();
        for (String str : strArr) {
            if (StringUtils.isEmpty(str)) {
                continue;
            }
            if (str.contains("/**")) {
                continue;
            }
            if (str.contains("*/")) {
                continue;
            }
            if (!str.contains("@")){
                builder.append(str);
            }
        }
        return builder.toString();
    }
}